import PieChart from '../PieChart/PieChart';

export default class IncidentHandler {
    constructor() {
        this.chart = null;
        this.infoObj = null;
        this._marker = null;
        this._popup = null;
        this._moveFeature = null
        this._clickFeature = null
    }

    loadData(map, isInitChart) {
        let that = this;
        minemap.util.getJSON('../data/incident.json', function (error, data) {
            const infoObj = data ? data || [] : [];
            that.infoObj = infoObj;
            if (isInitChart) {
                that.updateChart();
            }
        });
    }

    updateChart() {
        this.chart = new PieChart('incident-chart', this.infoObj);
    }

    getInfoObj() {
        return this.infoObj;
    }

    getChart() {
        return this.chart;
    }

    setVisible(map) {
        if (map.solution == 3756) {
            map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'visible');
        }
        if (map.solution == 4389) {
            map.setLayoutProperty('3423d2a936064f0b90561229a5cd8aba', 'visibility', 'visible');
        }
        // map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'visible');
        this._clear(map);
    }

    setUnvisible(map) {
        if (map.solution == 3756) {
            map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'none');
        }
        if (map.solution == 4389) {
            map.setLayoutProperty('3423d2a936064f0b90561229a5cd8aba', 'visibility', 'none');
        }
        // map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'none');
        this._clear(map);
    }

    onMouseMoveHandler(map, feature) {
        if (!map) {
            return;
        }
        if (this._popup) {
            this._popup.remove();
            this._popup = null;
        }
        if (!feature) {
            this._moveFeature = null;
        } else {
            this._moveFeature = feature;
            if (this._clickFeature) {
                const clickCoord = this._clickFeature.geometry.coordinates;
                const moveCoord = this._moveFeature.geometry.coordinates;
                if (clickCoord[0] == moveCoord[0] && clickCoord[1] == moveCoord[1]) {
                    this._moveFeature = null;
                } else {
                    if (this._marker) {
                        this._marker.remove();
                        this._marker = null;
                    }
                    this._clickFeature = null;
                }
            }
        }

        if (this._moveFeature) {
            let html = `<span>${this._moveFeature.properties['edesc'] || ''}</span>`;
            let el = document.createElement('div');
            el.className = 'function-info';
            el.innerHTML = html;
            this._popup = new minemap.Popup({
                closeOnClick: true,
                closeButton: false,
                offset: [0, 0]
            }).setLngLat(this._moveFeature.geometry.coordinates).setDOMContent(el).addTo(map);
        }
    }

    onMouseClickHandler(map, feature) {
        if (!map) {
            return;
        }
        if (this._popup) {
            this._popup.remove();
            this._popup = null;
        }
        if (this._marker) {
            this._marker.remove();
            this._marker = null;
        }
        if (!feature) {
            this._clickFeature = null;
        } else {
            this._moveFeature = null;
            this._clickFeature = feature;
        }

        if (this._clickFeature) {
            const _properties = this._clickFeature.properties;
            let el = document.createElement('div');
            el.className = 'function-detail';

            let dev0 = document.createElement('div');
            dev0.innerHTML = _properties['edesc'] || '';
            el.appendChild(dev0);

            let dev1 = document.createElement('div');
            dev1.innerHTML = `<span>开始时间：${_properties['stime'] || ''}</span>`;
            el.appendChild(dev1);

            let dev2 = document.createElement('div');
            dev2.innerHTML = `<span>结束时间：${_properties['exptime'] || ''}</span>`;
            el.appendChild(dev2);

            let dev3 = document.createElement('div');
            let esource = parseInt(_properties['esource'] || '0');
            let esourceName = "";
            if (esource == 0) {
                esourceName = "人工录入";
            } else if (esource == 1) {
                esourceName = "交通广播";
            } else if (esource == 2) {
                esourceName = "四维外业采集";
            } else if (esource == 3) {
                esourceName = "大数据挖掘";
            } else if (esource == 4) {
                esourceName = "接入";
            } else if (esource == 5) {
                esourceName = "交通眼";
            } else if (esource == 6) {
                esourceName = "大数据平台";
            } else if (esource == 7) {
                esourceName = "网络爬虫";
            } else if (esource == 101) {
                esourceName = "官方网站";
            } else if (esource == 102) {
                esourceName = "官方微信";
            } else if (esource == 103) {
                esourceName = "本地宝";
            } else if (esource == 104) {
                esourceName = "其他来源";
            } else if (esource == 105) {
                esourceName = "交通眼";
            } else if (esource == 106) {
                esourceName = "第三方合作";
            } else if (esource == 107) {
                esourceName = "语音识别";
            } else if (esource == 108) {
                esourceName = "外业采集";
            } else if (esource == 109) {
                esourceName = "预测性事件";
            }
            dev3.innerHTML = `<span>发布来源：${esourceName}</span>`;
            el.appendChild(dev3);
            this._marker = new minemap.Popup({
                closeOnClick: true,
                closeButton: false,
                offset: [0, 0]
            }).setLngLat(this._clickFeature.geometry.coordinates).setDOMContent(el).addTo(map);
        }
    }

    _clear(map) {
        if (map) {
            if (this._marker) {
                this._marker.remove();
            }
            if (this._popup) {
                this._popup.remove();
            }
        }
        this._marker = null;
        this._popup = null;
        this._moveFeature = null
        this._clickFeature = null
    }
};